<?php

/**
 * 机型管理
 *  
 */ 

class modelAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
    }
    
    public function _before_index() {  
        $big_menu = array(
            'title' => L('model_add'),
            'iframe' => U(MODULE_NAME.'/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '300'
        );
        $this->assign('big_menu', $big_menu);        
       
        $this->readyData();   
    }
    
    public function _before_add(){
        $this->readyData(); 
    }
    public function _before_edit(){       
        $this->readyData(); 
    }
    
    public function ajax_check_name() {
        $name = $this->_get('J_name', 'trim');
        $id   = $this->_get('id', 'intval');
        $id = intval($id);
        $res = M('model')->where('name="'.$name.'" and `id`!='.$id)->find();
        if ($res) {
            echo 0;
            //$this->ajaxReturn(0);
        } else {
            echo 1;
            
            //$this->ajaxReturn(1);
        }
    }
    
    private function readyData(){
        //查数据
        $brandList = F('brand_list');
        if(empty($brandList)){
            $brandList = D('brand')->cate_cache();
        }    
        $this->assign('brandList', $brandList);         
    }
    
    
    /**
     * 获取请求参数生成条件数组
     */
    protected function _search() {
        //生成查询条件       
        ($name     = $this->_request('name', 'trim')) && $map['name'] = array('like', '%'.$name.'%');  
        ($brandId  = $this->_request('brandId', 'intval')) && $map['brandId'] = $brandId;
         $isTest    = $this->_request('isTest');
         $isImportant = $this->_request('isImportant');
         $isHead    = $this->_request('isHead');
         $status    = $this->_request('status');
        if(is_numeric($isTest)){
            $map['isTest'] = intval($isTest);
        }
        if(is_numeric($isHead)){
            $map['isHead'] = intval($isHead);
        }        
        if(is_numeric($isImportant)){
            $map['isImportant'] = intval($isImportant);
        } 
        if(is_numeric($status)){
            $map['status'] =intval($status);
        }
        $this->assign('search', array(
            'status'  => $status,
            'name' => $name,
            'isTest'=>$isTest,
            'isImportant'=>$isImportant,
            'isHead'=>$isHead,
            'brandId'=>$brandId,
        ));                     
        return $map;
    }
    
     /**
     * 取得指定品牌下面的机型
     */
    public function getModelList(){
        $brandId = $this->_request('brand_id','intval');
      
        
        $list = M(MODULE_NAME)->where(array('brandId'=>$brandId))->getField('id,name');
        if(!empty($list)){
            $this->ajaxReturn(1,'',$list);
        }else{
            $this->ajaxReturn(0);
        } 
    }   
    
}

?>